<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-02-25 18:01:04
 * @LastEditors: Sam
 * @LastEditTime: 2021-09-08 21:43:34
 */
namespace app\__MODULE__\model;
use think\Model;

class SurveyQuestion extends Model {
    
    /**
     * 数据表主键
     * @var string
     */

    protected $pk = 'id';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    
     /**
     * 模型名称
     * @var string
     */
    protected $name = 'survey_question';

 
    public function survey(){
        return $this->belongsTo('survey_theme','survey_id')->bind(['survey_title'=>'title']);
    }
    
    public function option(){
        return $this->hasMany('survey_option','question_id');
    }
    

    public function getOptionAttr($value,$data){
        return SurveyOption::where('question_id',$data['id'])->field('id as option_id,option_name,option_number')->select();
    }
    /**
     * 异步获取当前列表 信息
     * @param $where
     * @return array
     */
    public static function getList($where)
    {
        $data=self::setWhere($where)->page((int)$where['page'],(int)$where['limit'])->order('id desc')->select();
        $data=count($data) ? $data->toArray() : [];
        $count=self::setWhere($where)->count();
        return compact('data','count');
    }

    /**
     * 获取查询条件
     * @param $where
     * @param string $alert
     * @param null $model
     * @return SystemUserLevel|null
     */
    public static function setWhere($where,$alert='',$model=null)
    {
        $model=$model===null ? self::with('survey') : $model;
        if(isset($where['title']) && $where['title']!='') $model = $model->where('title','like',"%{$where['title']}%");
        if(isset($where['survey']) && $where['survey']!='') $model = $model->where('survey_id','=',$where['survey']);
        if(isset($where['order']) && !empty(trim($where['order']))){
            $order = self::setOrder($where['order']);
            $model = $model->order($order);
        }
        return $model;
    }
}

